// 1.创建的登陆模块，并创建登陆控制器
var loginApp = angular.module('loginApp',[]);
loginApp.controller('loginController',function ($scope,$http) {
    // 数据
    $scope.formData= {};
    // 发送方式
    $scope.postForm = function () {
        // 1.formData添加一个属性，action代表的就是请求的行为
        $scope.formData.action = 'login';
        $http({
            method:"POST",
            url:'./get.php',
            data:$.param($scope.formData),
            headers:{'Content-Type':'application/x-www-form-urlencoded'}
        }).success(function (data) {
            // console.log(data);
            if(!data.success){
                if(!data.errors){
                    $scope.message = data.message;
                }else {
                    $scope.errorUsername = data.errors.username;
                    $scope.errorPassword = data.errors.password;
                }
            }else {
                // 当前登陆成功
                window.location.href='#/0';
            }
        })
    }
})
// 列表页面的模块
var pageList = angular.module('pageList',[]);
pageList.controller('ListTypeCtrl',function ($scope,$http) {
    $http({
        method:'GET',
        url:'get.php?action=get_arctype&where=reid=0'
    }).success(function(data){
        $scope.lists = data;
    }).error(function(err){
        console.log('err');
    })
})
// 文章列表的控制器

total = 0;
pageList.controller('arcListCtrl',function ($scope,$http,$location) {
    $scope.typeid = $location.path().replace('/','');
    //然后获取文章的总数量
    if($scope.typeid == 0){
        $get_total_url = 'get.php?action=get_total'
    }else{
        $get_total_url = 'get.php?action=get_total&where=typeid=' + $scope.typeid;
    }
    //发送请求，得到文章的总数量
    $http({
        method:'GET',
        url:$get_total_url
    }).success(function(data){
        $scope.paginationConf.totalItems = data.total;
    }).error(function(data){
        console.log('error....')
    })
    // 设置分页的对象
    $scope.paginationConf = {
        // 当前的页数
        currentPage:1,
        // 每页条数
        itemsPerPage:5,
        // totalItems 是文章总数量
        pagesLength:5,
        // 可以自动更换的每页多少条
        perPageOptions:[ 10, 20, 30, 40, 50],
        rememberPerPage:'perPageItems',
        onChange:function () {
            // 先判断一下当前页数，是否是第一页，获取到查询的起始位置
            if($scope.paginationConf.currentPage == 1){
                // 当前第一页
                $scope.limit = 0;
            }else{
                //第一页的话,0 ,5 第二页 5,10,第三页 10,15
                // 当前页数减一*每页多少条
                $scope.limit = $scope.paginationConf.currentPage *
                    $scope.paginationConf.itemsPerPage -
                    $scope.paginationConf.itemsPerPage
            }
            //根据当前的type类型，显示不同的栏目里面的内容,先显示的就是第一页的五条数据
            if($scope.typeid == 0){
                $geturl = 'get.php?action=get_list&offset=' + $scope.limit + '&rows=' + $scope.paginationConf.itemsPerPage + '&orderField=id&orderBy=DESC';
            }else{
                $geturl='get.php?action=get_list&offset='+$scope.limit+'&rows='+$scope.paginationConf.itemsPerPage+'&where=typeid='+$scope.typeid+'&orderField=id&orderBy=DESC';
            }
            // 最后发送请求
            $http({
                method:'GET',
                url:$geturl,
            }).success(function(data,status,headers,config){
                $scope.lists = data;
                // console.log(data);
            }).error(function(data,status,headers,config){
                console.log('error....')
            })
        }


    }
//删除模块
    $scope.del = function (index,id) {
        $http({
            metdod:'GET',
            url:'get.php?action=delete_article&index='+ index + '&id=' + id,
        }).success(function (data) {
            if (data.code==101) {
                //删除成功
                //console.log('删除成功');
                $scope.meg_success="删除成功!";
                $scope.meg_error="";
//                setTimeout(function(){location.href='#/list/0'}, 1000);
                //重新发送ajax请求 页面
                $http({
                    method: 'GET',
                    url: $geturl,
                }).success(function (data) {
                    $scope.lists = data;
                }).error(function (err) {
                })
                $http({
                    method: 'GET',
                    url: $get_total_url
                }).success(function (data) {
                    $scope.paginationConf.totalItems = data.total;
                }).error(function (err) {
                    console.log(err);
                })

            } else {
                //添加失败
                //console.log('删除失败');
                $scope.meg_error="删除失败";
            }
        })
    }

})
// 新增
var addCont = angular.module('addCont',[]);
addCont.controller('AddContCtrl',function ($scope,$http) {
    //获取分类的列表
    $http({
        method:'GET',
        url:'get.php?action=get_arctype&where=reid=0'
    }).success(function(data,status,headers,config){
        $scope.lists = data;
    }).error(function(data,status,headers,config){
        console.log('get type is error');
    })
    // 执行写入数据操作
    $scope.formData={};
    $scope.formData.action = 'add_article';
    $scope.postForm = function () {
        $http({
            method:'POST',
            url:'get.php',
            data:$.param($scope.formData),
            headers:{'Content-Type':'application/x-www-form-urlencoded'}

        }).success(function (data) {
            $scope.errorBye = function () {
                $('#errorbox').fadeOut();

            }
            $scope.errorShow=function () {
                $('#errorbox').fadeIn();
            }
            if (!data.errors){
                $scope.meg_success='插入成功！返回列表表面..';
                $scope.meg_error='';
                setTimeout(function () {
                    location.href='#/0'
                },1000);
            }else {
                // 添加失败
                $scope.meg_success='';
                var get_error = '';
                if (data.errors.hasOwnProperty('title')){
                    get_error=data.errors.title;
                }
                if (data.errors.hasOwnProperty('content')){
                    get_error=get_error+(get_error?"&":"")+data.errors.content;
                }
                if (data.errors.hasOwnProperty('typeid')){
                    get_error = get_error+(get_error?"&":"")+data.errors.typeid;

                }
                $scope.meg_error=get_error;
            }
        })
    }
})
// 修改模块
var modifyCont = angular.module('modifyCont',[]);
modifyCont.controller('modifyContCtrl',function ($scope,$http,$stateParams) {
    // 获取分类列表
    $http({
        method:'GET',
        url:'get.php?action=get_arctype&where=reid=0'
    }).success(function (data) {
        $scope.types=data;
    })
    // 读取当前的文章信息
    console.log($stateParams.Id);
    $http({
        method:"GET",
        url:'get.php?action=get_article&id=' + $stateParams.Id
    }).success(function (data) {
        // console.log(data);
        $scope.lists=data;

    }).error(function (data) {
        // console.log('data')
    })
    // postForm操作
    $scope.formData = {};
    $scope.postForm = function () {
        $scope.formData.action = 'update_article';
        $scope.formData.id = $stateParams.Id;
        $scope.formData.title = form.title.value;
        $scope.formData.content = form.content.value;
        $scope.formData.typeid = $('#typeid option:selected').val();
        // 发送数据
        $http({
            method:"POST",
            url:'get.php',
            data:$.param($scope.formData),
            headers:{'Content-Type':'application/x-www-form-urlencoded'}
        }).success(function (data) {
            console.log(data);
            if (data.code==101){
                $scope.meg_success = '修改成功';
                $scope.meg_error = '';
                setTimeout(function () {location.href = '#/0'},1000)
            }else {
                var meg_success = '';
                var get_error = '';
                $scope.errorBye = function () {
                    $('#errorbox').fadeOut();
                }
                $scope.errorShow = function () {
                    $('#errorbox').fadeIn();
                }
                if (data.errors){
                    if (data.errors.hasOwnProperty('title')){
                        get_error = data.errors.title;
                    }
                    if (data.errors.hasOwnProperty('content')){
                        get_error = data.errors.content;
                    }
                    $scope.meg_error=get_error;
                }else {
                    $scope.meg_error = '修改失败，无任何改动';
                }
            }

        })
    }
})
// 详情模块
var showCont = angular.module('showCont',[]);
showCont.controller('showContCtrl',function ($scope,$http,$stateParams) {
   $http({
       method:'GET',
       url:'get.php?action=get_article&id='+$stateParams.Id
   }).success(function (data) {
       $scope.lists=data;
   }).error(function (err) {
       console.log(err);
   })
})
